﻿var zombies = zombies || {};

zombies.channel = function () {
    var self = this;
    
    //collections
    self.RecentUploads = ko.observableArray();
    self.LikedAdvs = ko.observableArray();
    self.ViewsAdvs = ko.observableArray();

    self.RecentPage = ko.observable(1);
    self.LikesPage = ko.observable(1);
    self.ViewsPage = ko.observable(1);
    
    //more ?
    self.IsMoreRecent = ko.observable(false);
    self.IsMoreLiked = ko.observable(false);
    self.IsMoreViews = ko.observable(false);

    self.doMoreRecent = function () {
        var page = self.RecentPage() + 1;
        loadRecentUploads(pubId, page, 10, 0);
        self.RecentPage(page);
    };

    self.doMoreLiked = function () {
        var page = self.LikesPage() + 1;
        loadLikedAdvs(pubId, page, 10);
        self.LikesPage(page);
    };

    self.doMoreViews = function () {
        var page = self.ViewsPage() + 1;
        loadViewsAdvs(pubId, page, 10);
        self.ViewsPage(page);
    };
};

//load data functions
var loadRecentUploads = function (pubId, page, elementQ, oderBy) {
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetAdvByPublisherId/" + pubId,
        data: { page: page, elementQ: elementQ, orderBy: oderBy },
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        if (items.length === (elementQ + 1)) {
            items.pop();
            channelViewModel.IsMoreRecent(true);
        } else {
            channelViewModel.IsMoreRecent(false);
        }
        for (i in items) {
            if (items.hasOwnProperty(i)) {
                channelViewModel.RecentUploads.push(new zombies.Advertise(items[i]));
            }
        }
    }).fail(function () {
        toastr.error('Lỗi bất thường');
    });
};

var loadLikedAdvs = function (pubId, page, elementQ) {
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetAdvByPublisherId/" + pubId,
        data: { page: page, elementQ: elementQ, orderBy: 2 },
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        if (items.length === (elementQ + 1)) {
            items.pop();
            channelViewModel.IsMoreLiked(true);
        } else {
            channelViewModel.IsMoreLiked(false);
        }
        for (i in items) {
            if (items.hasOwnProperty(i)) {
                channelViewModel.LikedAdvs.push(new zombies.Advertise(items[i]));
            }
        }
    }).fail(function () {
    });
};

var loadViewsAdvs = function (pubId, page, elementQ) {
    channelViewModel.ViewsAdvs.removeAll();
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetAdvByPublisherId/" + pubId,
        data: { page: page, elementQ: elementQ, orderBy: 3 },
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        if (items.length === (elementQ + 1)) {
            items.pop();
            channelViewModel.IsMoreViews(true);
        } else {
            channelViewModel.IsMoreViews(false);
        }
        for (i in items) {
            if (items.hasOwnProperty(i)) {
                channelViewModel.ViewsAdvs.push(new zombies.Advertise(items[i]));
            }
        }
    }).fail(function () {
    });
};

//get params
var pubId = location.href.split('/').pop();
//document ready/ Load view model
var channelViewModel;
$(document).ready(function () {
    channelViewModel = new zombies.channel();
    loadRecentUploads(pubId, channelViewModel.RecentPage(), 10 , 0);
    loadLikedAdvs(pubId, channelViewModel.LikesPage(), 10);
    loadViewsAdvs(pubId, channelViewModel.ViewsPage(), 10);

    ko.applyBindings(channelViewModel, document.getElementById("channel_content"));
});